package com.note.middleware.controller;

import com.note.common.vo.ResponseVo;
import com.note.middleware.dto.MinioUploadDto;
import com.note.middleware.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件控制器
 * 提供文件上传、下载等接口
 */
@Slf4j
@RestController
@RequestMapping("/file")
@Tag(name = "FileController", description = "文件服务接口")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 上传文件
     *
     * @param userId     用户ID（从请求头中获取）
     * @param file       上传的文件
     * @param businessId 关联业务ID（可选）
     * @return 上传结果
     */
    @PostMapping("/upload")
    @Operation(summary = "上传文件", description = "上传文件到MinIO对象存储")
    @Parameters({
            @Parameter(name = "X-User-Id", description = "用户ID", required = true, in = ParameterIn.HEADER),
            @Parameter(name = "businessId", description = "关联业务ID（可选）", in = ParameterIn.QUERY),
            @Parameter(name = "file", description = "上传的文件", required = true, in = ParameterIn.DEFAULT)
    })
    public ResponseVo<MinioUploadDto> uploadFile(
            @RequestHeader("X-User-Id") Long userId,
            @RequestParam(value = "businessId", required = false) String businessId,
            @RequestPart @RequestParam("file") MultipartFile[] file) {

        log.info("文件上传请求: userId={}, businessId={}, fileName={}, fileSize={}",
                userId, businessId, file[0].getOriginalFilename(), file[0].getSize());

        return fileService.uploadFile(userId, file[0], businessId);
    }

    /**
     * 上传头像
     * 专用接口，简化头像上传
     *
     * @param userId 用户ID
     * @param file   头像文件
     * @return 上传结果
     */
    @PostMapping("/avatar")
    @Operation(summary = "上传头像", description = "上传用户头像")
    @Parameters({
            @Parameter(name = "X-User-Id", description = "用户ID", required = true, in = ParameterIn.HEADER),
            @Parameter(name = "file", description = "头像文件", required = true, in = ParameterIn.DEFAULT)
    })
    public ResponseVo<MinioUploadDto> uploadAvatar(
            @RequestHeader("X-User-Id") Long userId,
            @RequestPart @RequestParam("file") MultipartFile[] file) {

        log.info("头像上传请求: userId={}, fileName={}, fileSize={}",
                userId, file[0].getOriginalFilename(), file[0].getSize());

        // 使用"avatar"作为业务ID
        return fileService.uploadFile(userId, file[0], "avatar");
    }
} 